package edu.msu.project3.geodraw;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class DrawingView extends View {
	
	private Paint circlePaint;
	private Paint linePaint;
	
	public int getLinePaintColor() {
		return linePaint.getColor();
	}

	public void setLinePaintColor(int color) {
		this.linePaint.setColor(color);
	}

	private float xLocation = 0.0f;
	private float yLocation = 0.0f;
	
	private int canvasWidth = 0;
	private int canvasHeight = 0;
	
	private float mlineWidth=30.0f;
	
//	private ArrayList<Point> coordinates = new ArrayList<Point>();
	
	private ArrayList<Drawing> drawings = new ArrayList<Drawing>();
	
	Drawing currentDrawing = null;
	
	private boolean allowDrawing=false;
	
	private class Point {
		private float x;
		private float y;
	}

	public DrawingView(Context context) {
		super(context);
		init(context);
	}
	
	public DrawingView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public DrawingView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}
	
	private void init(Context context) {
		circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		circlePaint.setColor(Color.BLACK);
		circlePaint.setStrokeWidth(4.0f);
		
		linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		linePaint.setColor(Color.BLACK);
		linePaint.setStrokeWidth(4.0f);
		
		drawings.clear();
		
		currentDrawing = new Drawing();
		currentDrawing.setLinePaint(linePaint);
		currentDrawing.addPoint(canvasWidth/2, canvasHeight/2);
		
		drawings.add(currentDrawing);
	}

	public void clearCoordinates(){
//		coordinates.clear();
		Point origin = new Point();
		origin.x = canvasWidth/2;
		origin.y = canvasHeight/2;
//		coordinates.add(origin);
		
		drawings.clear();
		
		currentDrawing = new Drawing();
		currentDrawing.setLinePaint(linePaint);
		currentDrawing.addPoint(canvasWidth/2, canvasHeight/2);
		
		drawings.add(currentDrawing);
	}
	
	@Override
	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawCircle((canvas.getWidth()/2)+xLocation, (canvas.getHeight()/2)+yLocation, 10, circlePaint);
		
//		int size = coordinates.size();
//
//		for (int i=1; i<size; i++){
//			Point currPoint = coordinates.get(i);
//			Point lastPoint = coordinates.get(i-1);
//			canvas.drawLine(lastPoint.x, lastPoint.y, currPoint.x, currPoint.y, linePaint);
//		}
		Drawing prevDrawing = null;
		for (Drawing drawing : drawings) {
			drawing.DrawLine(canvas);
			
			if (prevDrawing != null) {
				if(prevDrawing.isCut){
					
					Drawing.Point endPoint = drawing.getFirstPoint();
					Drawing.Point startPoint = prevDrawing.getLastPoint();
					
					canvas.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y, prevDrawing.getLinePaint());
				}
			}
			
			prevDrawing = drawing;
		}
	}
	
	public void updatePosition(float xRatio, float yRatio) {		
		
		xLocation = xRatio*(canvasWidth/2);	
		yLocation = yRatio*(canvasHeight/2);
			
		Point point = new Point();
		point.x = xLocation+canvasWidth/2;
		point.y = yLocation+canvasHeight/2;
		//		coordinates.add(point);
		currentDrawing= new Drawing();
		currentDrawing.setLinePaint(linePaint);
		currentDrawing.setWidth(mlineWidth);
		
		currentDrawing.addPoint(point.x, point.y);
		currentDrawing.isCut=allowDrawing;
		
		drawings.add(currentDrawing);
		invalidate();

	}

	@Override
	public void onSizeChanged(int w, int h, int oldw, int oldh) {
		canvasWidth = w;
		canvasHeight = h;
		super.onSizeChanged(w, h, oldw, oldh);
	}
//	public int getCanvasWidth() {
//		return canvasWidth;
//	}
//
//	public void setCanvasWidth(int canvasWidth) {
//		this.canvasWidth = canvasWidth;
//	}
//
//	public int getCanvasHeight() {
//		return canvasHeight;
//	}
//
//	public void setCanvasHeight(int canvasHeight) {
//		this.canvasHeight = canvasHeight;
//	}

	public void SetLineWidth(float lineWidth){
		mlineWidth= lineWidth;
		linePaint.setStrokeWidth(lineWidth);
	}
	
	public void SetAllowDraw(boolean drawNow){
		allowDrawing= drawNow;
	}
	
}
